package jointpart.assignment3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import shared.WordList;


public class SearchCmd3 {

	public static void main (String[] args) throws IOException {
		String name;

		// Check that a file name has been given as argument
		if (args.length < 1) {
			System.out.println("Usage: java SearchCmd <datafile>");
			System.exit(1);
		}

		// Read the file and create the linked list
		WordList l = Searcher3.readWordList (args[0]);

		// if output file defined in arguments - run benchmark
		if (args.length > 1){ 

			// create new benchmark test and run it
			Benchmark3 myBenchmark = new Benchmark3();
			myBenchmark.runBenchmark( l, args[0], args[1]);
		}

		// Ask for a word to search
		BufferedReader inuser =
				new BufferedReader (new InputStreamReader (System.in));

		System.out.println ("Hit return to exit.");
		boolean quit = false;
		while (!quit) {
			System.out.print ("Search for: ");
			// Read a line from the terminal
			name = inuser.readLine(); 

			//Search for word
			WordList tempList = Searcher3.findWord (l, name);

			//If no search query is given
			if (name == null || name.length() == 0) {
				//End application
				quit = true;

				//If the word is found
			} else if (tempList != null) {
				System.out.println(name + " found on following pages:");
				//Print urls
				System.out.println(tempList.nexturl.toString()); 

				//If the word is not found
			} else {
				System.out.println ("The word \""+name+"\" has NOT been found.");
			}
		}
	}
}